<template>
  <!-- Excel 导入功能组件 -->
  <upload-excel :on-success="handleSuccess" />
</template>

<script>
import { importEmployeeAPI } from '@/api/employees'
import { formatExcelDate } from '@/utils/index.js'
export default {
  name: 'Excel',
  methods: {
    // 导入成功以后的回调函数
    async handleSuccess({ results, header }) {
      console.log(results)
      console.log(header)
      const obj = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      const arr = []
      results.forEach(item => {
        const newObj = {}
        Object.keys(item).forEach(key => {
          if (obj[key] === 'correctionTime' || obj[key] === 'timeOfEntry') {
            newObj[obj[key]] = formatExcelDate(item[key])
          } else { newObj[obj[key]] = item[key] }
        })
        arr.push(newObj)
      })
      console.log(arr)
      const { message, success } = await importEmployeeAPI(arr)
      if (success) this.$message.success(message)
      else this.$message.error(message)
      this.$router.push('/employees')
    }
  }
}
</script>

<style lang="scss" scoped></style>
